package cc.rengu.igas.mcps.core.service.trans.common;

import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.service.base.ManageService;
import cc.rengu.igas.mcps.facade.request.PosSignOutRequest;
import cc.rengu.igas.mcps.facade.response.PosSignOutResponse;
import cc.rengu.igas.share.core.model.MchntTermInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;

/**
 * POS签退 M00MC002
 */
public class TermSignOutService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        PosSignOutRequest posSignOutRequest = new PosSignOutRequest();
        ConvertUtil.convertOutput(posSignOutRequest);
        return posSignOutRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        PosSignOutRequest posSignOutRequest = (PosSignOutRequest) request;
        /* 获取终端信息 */
        MchntService mchntService = new MchntServiceImpl();
        MchntTermInfo mchntTermInfo = mchntService.getMchntTermInfo(posSignOutRequest.getHeader().getInstId(), posSignOutRequest.getMchntNo(), posSignOutRequest.getTermNo());
        /* Step 1 ：检查终端状态*/
        /*获取终端状态*/
        String signSta = mchntTermInfo.getTermSignStatus();
        /*判断状态是否正确*/
        if (!"1".equals(signSta)) {
            rglog.error("终端状态异常");
            throw new BizException(RespCodeEnum.TERM_STATUS_ABNORMAL.getRespCode(), RespCodeEnum.TERM_STATUS_ABNORMAL.getRespDesc());
        }
        /* Step 2 ：更新终端状态*/
        /*修改终端状态*/
        mchntTermInfo.setTermSignStatus("0");
        /*更新*/
        boolean updateFlag = mchntService.updateMchntTermInfo("01", mchntTermInfo);
        if (!updateFlag) {
            rglog.error("更新终端状态失败");
            throw new BizException(RespCodeEnum.UPDATE_TERM_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_TERM_INFO_ERROR.getRespDesc());
        }

        /* Step 3 ：重组响应结果 */
        /*创建response报文对象*/
        PosSignOutResponse posSignOutResponse = new PosSignOutResponse();
        ConvertUtil.convertOutput(posSignOutResponse);
        posSignOutResponse.setBatchNo(mchntTermInfo.getBatchNo());
        /*创建biz response*/
        BizResponse bizResponse = new BizResponse();
        /*设置属性值*/
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(posSignOutResponse);
        /*返回response*/
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        PosSignOutResponse posSignOutResponse = (PosSignOutResponse) bizResponse.getResult();
        ConvertUtil.convertInput(posSignOutResponse);
    }
}
